package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.shop.bo.CartBO;
import com.junzhiit.nengji.shop.dao.CartDAO;
import com.junzhiit.nengji.shop.model.Cart;
import com.junzhiit.nengji.shop.model.Shop;
import com.junzhiit.nengji.shop.model.enums.GoodsImagesType;
import com.junzhiit.nengji.shop.model.enums.Is;
import com.junzhiit.nengji.shop.model.enums.OrderType;
import com.junzhiit.nengji.shop.model.enums.Status;

@Repository
public class CartDAOImpl extends AbstractBaseDAO<Cart> implements CartDAO {

	@Override
	public List<Shop> getCartShop(String userId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select distinct s.*");
		sql.append(" from cart c");
		sql.append(" inner join shop s on c.shop_id=s.id");
		sql.append(" where s.is_delete = ? and c.is_delete=? and c.user_id =? ");

		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		params.setInt(IsDelete.NO.toIntValue());
		params.setString(userId);

		return queryForList(sql.toString(), Shop.class, params);
	}

	@Override
	public List<CartBO> findAll(String userId,String shopId,Integer isPlatform,Integer isSynchronous) {
		StringBuffer sql = new StringBuffer();
		sql.append("select c.id,c.shop_id,c.user_id,c.goods_id,c.sku_content_id,c.number,");
		sql.append("c.status,s.name 'shop_name',s.shop_head_img_url,sc.key,sc.standards,");
		sql.append("g.code,g.name 'goods_name',g.hint,g.reference_price,g.is_open_member,");
		sql.append("g.is_free_shipping,g.is_recommend,g.is_open_rebate,g.is_open_sku,g.postage_type,");
		sql.append("(case g.is_open_sku when ? then gi.file_name when ? then sc.pic end) pic,");
		sql.append("(case g.is_open_sku when ? then g.stock when ? then sc.stock end) stock,");
		sql.append("(case g.is_open_sku when ? then g.price when ? then sc.price  end) price,");
		sql.append("g.postage_price,g.postage_template_id,g.postage_weight,gi.file_name");
		sql.append(" from cart c");
		sql.append(" inner join shop s on c.shop_id=s.id");
		sql.append(" inner join goods g on c.goods_id=g.id");
		sql.append(" left join goods_images gi on g.id=gi.goods_id and gi.type=? ");
		sql.append(" left join sku_content sc on c.sku_content_id=sc.id");
		sql.append(" where c.is_delete = ? and g.type!=? and ( c.user_id =? ");

		StatementParameter params = new StatementParameter();
		params.setInt(Is.NO.toIntValue());
		params.setInt(Is.YES.toIntValue());
		params.setInt(Is.NO.toIntValue());
		params.setInt(Is.YES.toIntValue());
		params.setInt(Is.NO.toIntValue());
		params.setInt(Is.YES.toIntValue());
		params.setInt(GoodsImagesType.COVER.toIntStatus());
		params.setInt(IsDelete.NO.toIntValue());
		params.setInt(OrderType.EAT.toIntValue());
		params.setString(userId);
		
		if(StringUtils.isNotBlank(shopId)){
			sql.append(" and s.id = ? ");
			params.setString(shopId);
		}
		
		if(isPlatform != null){
			sql.append(" and s.is_platform = ? ");
			params.setInt(isPlatform);
		}
		
		if(isSynchronous !=null){
			sql.append(" or g.is_synchronous = ? ");
			params.setInt(isSynchronous);
		}
		sql.append(" ) ");
		return queryForList(sql.toString(), CartBO.class, params);
	}
	
	@Override
	public List<CartBO> findEatAll(String userId,String shopId,Integer isPlatform,Integer isSynchronous,Integer type) {
		StringBuffer sql = new StringBuffer();
		sql.append("select c.id,c.shop_id,c.user_id,c.goods_id,c.sku_content_id,c.number,");
		sql.append("c.status,s.name 'shop_name',s.shop_head_img_url,sc.key,sc.standards,");
		sql.append("g.code,g.name 'goods_name',g.hint,g.reference_price,g.is_open_member,");
		sql.append("g.is_free_shipping,g.is_recommend,g.is_open_rebate,g.is_open_sku,g.postage_type,");
		sql.append("(case g.is_open_sku when ? then gi.file_name when ? then sc.pic end) pic,");
		sql.append("(case g.is_open_sku when ? then g.stock when ? then sc.stock end) stock,");
		sql.append("(case g.is_open_sku when ? then g.price when ? then sc.price  end) price,");
		sql.append("g.postage_price,g.postage_template_id,g.postage_weight,gi.file_name");
		sql.append(" from cart c");
		sql.append(" inner join shop s on c.shop_id=s.id");
		sql.append(" inner join goods g on c.goods_id=g.id");
		sql.append(" left join goods_images gi on g.id=gi.goods_id and gi.type=? ");
		sql.append(" left join sku_content sc on c.sku_content_id=sc.id");
		sql.append(" where c.is_delete = ? ");

		StatementParameter params = new StatementParameter();
		params.setInt(Is.NO.toIntValue());
		params.setInt(Is.YES.toIntValue());
		params.setInt(Is.NO.toIntValue());
		params.setInt(Is.YES.toIntValue());
		params.setInt(Is.NO.toIntValue());
		params.setInt(Is.YES.toIntValue());
		params.setInt(GoodsImagesType.COVER.toIntStatus());
		params.setInt(IsDelete.NO.toIntValue());
		
		if(type!=null){
			sql.append(" and g.type = ? ");
			params.setInt(type);
		}
		
		sql.append(" and ( c.user_id =?  ");
		params.setString(userId);
		
		if(StringUtils.isNotBlank(shopId)){
			sql.append(" and s.id = ? ");
			params.setString(shopId);
		}
		
		if(isPlatform != null){
			sql.append(" and s.is_platform = ? ");
			params.setInt(isPlatform);
		}
		
		if(isSynchronous !=null){
			sql.append(" or g.is_synchronous = ? ");
			params.setInt(isSynchronous);
		}
		sql.append(" ) ");
		return queryForList(sql.toString(), CartBO.class, params);
	}
	
	public List<CartBO> getCartExtendInfoById(String cart){
		StringBuffer sql = new StringBuffer();
		sql.append("select c.id,c.shop_id,c.user_id,c.goods_id,c.sku_content_id,c.number,");
		sql.append("c.status,s.name 'shop_name',s.shop_head_img_url,sc.key,sc.standards,");
		sql.append("g.code,g.name 'goods_name',g.hint,g.food_box_charge,g.reference_price,g.is_open_member,");
		sql.append("g.is_free_shipping,g.is_recommend,g.is_open_rebate,g.is_open_sku,g.postage_type,");
		sql.append("(case g.is_open_sku when ? then gi.file_name when ? then sc.pic end) pic,");
		sql.append("(case g.is_open_sku when ? then g.stock when ? then sc.stock end) stock,");
		sql.append("(case g.is_open_sku when ? then g.price when ? then sc.price  end) price,");
		sql.append("g.postage_price,g.postage_template_id,g.postage_weight,gi.file_name,g.is_open_sell,g.discount");
		sql.append(" from cart c");
		sql.append(" inner join shop s on c.shop_id=s.id");
		sql.append(" inner join goods g on c.goods_id=g.id");
		sql.append(" left join goods_images gi on g.id=gi.goods_id and gi.type=? ");
		sql.append(" left join sku_content sc on c.sku_content_id=sc.id");
		sql.append(" where c.is_delete = ? and c.id in ("+cart+")");

		StatementParameter params = new StatementParameter();
		params.setInt(Is.NO.toIntValue());
		params.setInt(Is.YES.toIntValue());
		params.setInt(Is.NO.toIntValue());
		params.setInt(Is.YES.toIntValue());
		params.setInt(Is.NO.toIntValue());
		params.setInt(Is.YES.toIntValue());
		params.setInt(GoodsImagesType.COVER.toIntStatus());
		params.setInt(IsDelete.NO.toIntValue());
		
		return queryForList(sql.toString(), CartBO.class, params);
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	@Override
	public boolean updateStatus(String goodsId, int status) {
		StringBuffer sql = new StringBuffer();
		sql.append(" update cart set status=?");
		sql.append(" where goods_id=?");

		StatementParameter param = new StatementParameter();
		param.setInt(status);
		param.setString(goodsId);

		return super.updateForBoolean(sql.toString(), param);
	}

	@Override
	public Cart findCart(Cart cart) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from cart where is_delete=? and status = ? ");

		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		params.setInt(Status.ACTIVE.toIntStatus());
		
		if(cart!=null){
			if(StringUtils.isNotBlank(cart.getGoodsId())){
				sql.append(" and goods_id=?");
				params.setString(cart.getGoodsId());
			}
			if(StringUtils.isNotBlank(cart.getUserId())){
				sql.append(" and user_id=?");
				params.setString(cart.getUserId());
			}
			if(StringUtils.isNotBlank(cart.getSkuContentId())){
				sql.append(" and sku_content_id=?");
				params.setString(cart.getSkuContentId());
			}
		}
		
		return query(sql.toString(), Cart.class, params);
	}

	@Override
	public Integer sumGoodsNumber(String userId,String shopId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select ifnull(sum(number),0) from cart where is_delete=? and user_id= ? ");

		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		params.setString(userId);
		
		if(shopId!=null){
			sql.append(" and shop_id = ? ");
			params.setString(shopId);
		}

		return queryForInt(sql.toString(), params);
	}
	
	public int batchDeleteGoods(String[] ids) {
		StringBuilder sql = new StringBuilder();
		StatementParameter params = new StatementParameter();
		sql.append("delete from cart where id in(");
		for (int i= 0;i<ids.length;i++) {
			if(i == 0){
				sql.append("?");
			}else{
				sql.append(",?");
			}
			params.setString(ids[i]);
		}
		sql.append(")");
		return super.merge(sql.toString(), params);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public int batchDeleteGoods(String[] goodsId, String usrtId) {
		StringBuilder sql = new StringBuilder();
		StatementParameter params = new StatementParameter();
		sql.append("delete from cart where user_id = ? and goods_id in(");
		params.setString(usrtId);
		for (int i= 0;i<goodsId.length;i++) {
			if(i == 0){
				sql.append("?");
			}else{
				sql.append(",?");
			}
			params.setString(goodsId[i]);
		}
		sql.append(")");
		return super.merge(sql.toString(), params);
	}

}
